<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">一体杆名称</div>
      <el-input v-model="params.poleName" clearable placeholder="请输入一体杆名称" class="search-main" @clear="clearSearch" />
      <div class="search-label">一体杆编号</div>
      <el-input v-model="params.poleNumber" clearable placeholder="请输入一体杆编号" class="search-main" @clear="clearSearch" />
      <div class="search-label">状态</div>
      <el-select v-model="params.poleStatus" clearable placeholder="未选择" @clear="clearSearch">
        <el-option
          v-for="item in roleList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" @click="doSearch">查询</el-button>
    </div>
    <!-- 添加 -->
    <div class="create-container">
      <el-button type="primary" @click="tianjia()">添加一体杆</el-button>
    </div>
    <!-- 表格 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-checkbox v-model="checkAll">全选</el-checkbox>
        <div style="margin: 15px 0;" />
        <el-table-column type="index" label="序号" />
        <el-table-column label="一体杆名称" width="180" prop="poleName" />
        <el-table-column label="一体杆编号" width="180" prop="poleNumber" />
        <el-table-column label="一体杆IP" width="120" prop="poleIp" />
        <el-table-column label="安装区域" width="120" prop="areaName" />
        <el-table-column label="一体杆类型" width="120" prop="poleType">
          <template #default="scope">
            {{ formatStatus(scope.row.poleType) }}
          </template>
        </el-table-column>
        <el-table-column label="运行状态" width="120" prop="poleStatus">
          <template #default="scope">
            {{ format(scope.row.poleStatus) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="bianji(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部分页 -->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @current-change="pageChange"
      />
    </div>
    <!-- 添加一体杆 -->
    <el-dialog
      title="添加一体杆"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
      @open="openDialog"
    >
      <!-- <span>这是一段信息</span> -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="一体杆名称" prop="poleName">
            <el-input v-model="addForm.poleName" />
          </el-form-item>
          <el-form-item label="一体杆编号" prop="poleNumber">
            <el-input v-model="addForm.poleNumber" />
          </el-form-item>
          <el-form-item label="一体杆IP" prop="poleIp">
            <el-input v-model="addForm.poleIp" />
          </el-form-item>
          <el-form-item label="安装区域" prop="areaId">
            <el-select v-model="addForm.areaId">
              <el-option
                v-for="item in options"
                :key="item.areaId"
                :label="item.areaName"
                :value="item.areaId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="一体杆类型" prop="poleType">
            <el-select v-model="addForm.poleType">
              <el-option
                v-for="item in dustyList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <!--  -->
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="queding">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { detailAPI, createuserAPI, getuserAPI, delEmployeeAPI, changeAPI } from '@/api/yiti'
export default {
  name: 'RodManage',
  data() {
    return {
      checkAll: false,
      options: [], // 安装区域
      dialogVisible: false, // 控制弹框打开关闭
      employeeList: [], // 页面渲染
      total: 0,
      params: {
        page: 1,
        pageSize: 10,
        poleName: '',
        poleNumber: '',
        poleStatus: '',
        areaId: null,
        poleType: ''
      },
      addForm: {
        poleName: '',
        poleNumber: '',
        poleIp: '',
        areaId: '',
        poleType: ''
      },
      addFormRules: {
        poleName: [{ required: true, message: '请输入一体杆名称', trigger: 'blur' }],
        poleNumber: [
          { required: true, message: '请输入一体杆编号', trigger: 'blur' }
        ],
        poleIp: [{ required: true, message: '请输入一体杆IP', trigger: 'blur' }],
        areaId: [
          { required: true, message: '请选择安装区域', trigger: 'blur' }
        ],
        poleType: [{ required: true, message: '请选择一体杆类型', trigger: 'blur' }]
      },
      // 一体杆类型
      dustyList: [{
        value: 'export',
        label: '出口'
      }, {
        value: 'entrance',
        label: '入口'
      }
      ],
      // 状态
      roleList: [
        { value: 0,
          label: '正常'
        }, {
          value: 1,
          label: '异常'
        }
      ]
    }
  },
  created() {
    this.list()
  },
  methods: {
    // 搜索
    doSearch() {
      this.params.page = 1
      this.list()
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.list()
    },
    clearSearch() {
      this.params.page = 1
      this.list()
    },
    // 状态
    formatStatus(status) {
      return status === 'export' ? '出口' : '入口'
    },
    format(status) {
      return status === 0 ? '正常' : '异常'
    },
    // 页面渲染
    async list() {
      const res = await getuserAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
      console.log(res)
    },
    // 添加
    async tianjia() {
      this.dialogVisible = true
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false
    },
    // 打开弹窗
    async openDialog() {
      const res = await detailAPI()
      console.log(res)
      this.options = res.data
    },
    // 提交
    queding() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.addForm.id) {
            await changeAPI(this.addForm)
          } else {
            const res = await createuserAPI(this.addForm)
            console.log(res)
          }
        }
        this.dialogVisible = false
        this.list()
      })
    },
    // 点击编辑
    bianji(row) {
      this.dialogVisible = true
      const { id, poleName, poleNumber, poleIp, areaId, poleType } = row
      this.addForm = { id, poleName, poleNumber, poleIp, areaId, poleType }
      console.log(id)
    },
    // 删除
    del(id) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployeeAPI(id)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.list()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.employee-container {
  padding: 10px;
   background-color: #f4f6f8;
   height: 100vh;
.table {
background-color: #f4f6f8;
  height: 100vh;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
}
</style>
